package com.chenss.dragonser.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chenss.dragonser.R;
import com.chenss.dragonser.app.AppInfo;
import com.chenss.dragonser.app.Constant;
import com.chenss.dragonser.base.BaseActivity;
import com.chenss.dragonser.model.RecordListModel;
import com.chenss.dragonser.network.NetworkRequest;
import com.chenss.dragonser.network.RequestCallbcak;
import com.chenss.dragonser.response.RecordListResponse;
import com.chenss.dragonser.utils.GsonUtil;
import com.chenss.dragonser.utils.ToastUtil;

import org.json.JSONException;

import java.util.List;
import java.util.logging.Logger;

/**
 * PageName: 提现
 * Create by chenss at 2018/4/20 2:54
 */
public class PostalActivity extends BaseActivity implements RequestCallbcak {
    LinearLayout ll_left;//左边按钮
    TextView tv_page_title;
    Button btn_sub;
    String store_wallet = "";
    TextView tv_money;
    EditText edt_amount;//提现金额
    TextView tv_get_code;//获取验证码按钮
    EditText edt_code;//验证码
    public NetworkRequest networkRequest;//网络请求
    public ArrayMap<String, String> parmMap;//请求参数封装

    @Override
    public void initParms(Bundle parms) {
        store_wallet = getIntent().getStringExtra("money");
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_postal;
    }

    @Override
    public void initView(View view) {
        ll_left = findView(R.id.ll_left);//左边按钮
        tv_page_title = findView(R.id.tv_page_title);
        ll_left.setVisibility(View.VISIBLE);
        ll_left.setOnClickListener(this);
        tv_page_title.setText("提现");

        tv_money = findView(R.id.tv_money);
        edt_amount = findView(R.id.edt_amount);
        tv_get_code = findView(R.id.tv_get_code);
        edt_code = findView(R.id.edt_code);

        btn_sub = findView(R.id.btn_sub);
        btn_sub.setOnClickListener(this);
        tv_get_code.setOnClickListener(this);
    }

    @Override
    public void doBusiness(Context mContext) {
        networkRequest = new NetworkRequest(mContext, this);
        tv_money.setText(store_wallet);
    }

    /**
     * 获取验证码
     */
    private void getVerCode() {
        /*
        'phone' => '17629001601',//17629001601
                'code_type' => 'cash', //验证码用途，忘记密码 forgotpassword 提现 cash 绑定手机号 bind
         */
        parmMap = new ArrayMap<>();
        Constant.setCommonParams(parmMap, Constant.App.Cas, Constant.classes.Cas_an_UserPhoneCode);
        parmMap.put("phone", AppInfo.getUser(this).getPhone());//电话
        parmMap.put("code_type", "cash");
        networkRequest.postJsonRequest(Constant.serverPath, parmMap, Constant.Actionid.NETWORK_REQUEST_CODE_1, true, Constant.classes.Cas_an_UserPhoneCode);
    }

    /**
     * 验证验证码
     */
    private void doCheckVerCode() {
        /*
        'phone' => '17629001601',
                'code_type' => 'cash', //验证码用途，忘记密码 forgotpassword 提现 cash 绑定手机号 bind
                'code' => '3573',
         */
        parmMap = new ArrayMap<>();
        Constant.setCommonParams(parmMap, Constant.App.Cas, Constant.classes.Cas_an_CheckCode);
        parmMap.put("phone", AppInfo.getUser(this).getPhone());//电话
        parmMap.put("code_type", "cash");
        parmMap.put("code", edt_code.getText().toString());
        networkRequest.postJsonRequest(Constant.serverPath, parmMap, Constant.Actionid.NETWORK_REQUEST_CODE_2, true, Constant.classes.Cas_an_CheckCode);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.ll_left:
                finish();
                break;

            case R.id.tv_get_code:
                if (TextUtils.isEmpty(edt_amount.getText().toString())) {
                    ToastUtil.showShort(this, "请输入提现金额");
                    return;
                }
                tv_get_code.setEnabled(false);
                tv_get_code.setClickable(false);
                timer.start();
                getVerCode();
                break;

            case R.id.btn_sub:
                if (TextUtils.isEmpty(edt_amount.getText().toString())) {
                    ToastUtil.showShort(this, "请输入提现金额");
                    return;
                }
                if (edt_amount.getText().toString().length() == 1) {
                    if (edt_amount.getText().toString().equals(".")) {
                        ToastUtil.showShort(this, "请输入正确的提现金额");
                        return;
                    }

                } else if (edt_amount.getText().toString().length() > 1) {
                    if (edt_amount.getText().toString().endsWith(".")) {
                        ToastUtil.showShort(this, "请输入正确的提现金额");
                        return;
                    }
                }
                if (Double.parseDouble(store_wallet) < Double.parseDouble(edt_amount.getText().toString()) || Double.parseDouble(edt_amount.getText().toString()) < 0) {
                    ToastUtil.showShort(this, "请输入正确的提现金额");
                    return;
                }
                if (TextUtils.isEmpty(edt_code.getText().toString())) {
                    ToastUtil.showShort(this, "请输入验证码");
                    return;
                }
                doCheckVerCode();
                break;
        }
    }

    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            tv_get_code.setText((millisUntilFinished / 1000) + "秒");
        }

        @Override
        public void onFinish() {
            tv_get_code.setEnabled(true);
            tv_get_code.setClickable(true);
            tv_get_code.setText("验证码");
        }
    };

    @Override
    public void onFaild(int orderCode, String result) {
        ToastUtil.showShort(this, result);
    }

    @Override
    public void onSucceed(int orderCode, String result) throws JSONException {
        Logger.getLogger(result);
        if (orderCode == Constant.Actionid.NETWORK_REQUEST_CODE_1) {
            ToastUtil.showShort(PostalActivity.this, "验证码已发送，请注意查收!");

        } else if (orderCode == Constant.Actionid.NETWORK_REQUEST_CODE_2) {
            Intent mIntent = new Intent(this, PostalWayActivity.class);
            mIntent.putExtra("amount", edt_amount.getText().toString());
            startActivity(mIntent);
            finish();
        }
    }

}
